#!/usr/bin/env python
#pre-commit hook for removing trailing whitespaces

import os
from subprocess import check_output
from subprocess import call
import re
import sys

# allowed file extensions to be checked
allowedExtensions = [".h", ".cc", ".msg", ".ned", ".tex"]

def isAllowedExt(filename):
    fileExt = os.path.splitext(filename)[1]
    return fileExt in allowedExtensions

def getStagedFiles():
    stagedFiles = check_output(["git","diff","--cached","--name-only"])
    stagedFiles = stagedFiles.split("\n")
    return [f for f in stagedFiles if f and isAllowedExt(f)]

stagedFiles = getStagedFiles()

for currentFile in stagedFiles:
    if not os.path.exists(currentFile):
        continue
    fr = open(currentFile,'r')
    sourceCode = fr.read()
    fr.close()

    modifiedSourceCode = re.sub('[ \t]+$','',sourceCode,0,re.MULTILINE) # trim trailing whitespaces
    modifiedSourceCode = modifiedSourceCode.replace('\t','    ') # replace tabs with four spaces

    if modifiedSourceCode != sourceCode:
        fw = open(currentFile,'w')
        fw.write(modifiedSourceCode)
        fw.close()
        check_output(["git", "add", currentFile])

sys.exit(0)
